package Persistance;

import Domain.Recommendation;

import java.util.*;

public class PageSplitter<T> {

    public Collection<T> splitCollectionToPages(Collection<T> c, int start, int amount) {
        int i = 0;
        int size = c.size() - start;
        if (size>amount)
            size = amount;
        if (size<=0)
            return new Vector<T>();
        Vector<T> res = new Vector<T>();
        for (T user: c) {
            if (i>=start && i<=start+amount) {
                res.add(user);
            }
            i++;
        }
        return res;
    }

    public SortedList<Double,T> splitSortedListToPages(Set<Map.Entry<Double,T>> c, int start, int amount, SortedList<Double, T> res) {
        int i = 0;
        int size = c.size() - start;
        if (size>amount)
            size = amount;
        if (size<=0)
            return res;
        for (Map.Entry<Double,T> e: c) {
            if (i>=start && i<=start+amount) {
                res.put(e.getKey(),e.getValue());
            }
            i++;
        }
        return res;
    }

    public Collection<String> filterStartWith(Collection<String> ans, String startWith) {
        Collection<String> res = new LinkedList<String>();
        for (String s: ans)
            if (s.startsWith(startWith))
                res.add(s);
        return res;
    }
}
